<?
//引入所有子系统信息
include_once("../../../common/script/extender_import.php");


//**获取当前板块资料，必然载入，后续很多读取板块数据库的表格，都写成动态的了。
$_plate=$plate['facebook'];



//获取板块的设置
include_once("../model/setting.php");




//根据页面数量信息，筛选信息，创造url，供页码,跳转等使用
include_once("script/set_url.php");


$optionsArray = array(
    '104932918958782' => 'wedding invitation',
    '580202941837101' => 'Wholesale of Wedding Planner',
    '607371405782562' => 'Wholesale of wedding decor',
    '596855730183461' => 'Wholesale of Wedding Guest Book',
    '640785835778820' => 'Enping Audio Factory',
    '577799722076689' => 'Custom Wedding Crafts'
);






//获取开始时间，结束时间，生成间隔，时间偏差，生成时间列表
//ai_period ai_deviation less_enddate greater_startdate

// 获取开始时间和结束时间，如果为空则设置为当前时间
$start_time = !empty($_REQUEST['greater_startdate']) ? $_REQUEST['greater_startdate'] : date('Y-m-d H:i:s');
$end_time = !empty($_REQUEST['less_enddate']) ? $_REQUEST['less_enddate'] :  date('Y-m-d H:i:s', strtotime('+1 day'));


// 获取生成间隔和时间偏差，如果为空则设置为默认值60
$period = !empty($_REQUEST['ai_period']) ? $_REQUEST['ai_period']*60 : 60*60;
$deviation = !empty($_REQUEST['ai_deviation']) ? $_REQUEST['ai_deviation']*60 : 60*15;



function get_time_list($start_time, $end_time, $period, $deviation)
{
    // 将字符串时间转换为时间戳
    $start_ts = strtotime($start_time);
    $end_ts = strtotime($end_time);

    $time_list = array();

    for ($time = $start_ts; $time <= $end_ts; $time += $period) {
        // 添加随机偏差
        $random_time = $time + mt_rand(-$deviation, $deviation);
        // 格式化为 Y-m-d H:i:s 格式
        $time_list[] = date('Y-m-d H:i:s', $random_time);
    }

    return $time_list;
}


$date_list=get_time_list($start_time,$end_time,$period,$deviation);






$_content_head=select_lines_all_items($table_prefix.'facebook_content_data',"","tag = '文章开头' ","");
$_content_head_array=transform_2darray_in_1darray_with_new_key($_content_head,'nid','content');

$_content_body=select_lines_all_items($table_prefix.'facebook_content_data',"","tag = '文章中间' ","");
$_content_body_array=transform_2darray_in_1darray_with_new_key($_content_body,'nid','content');

$_content_end=select_lines_all_items($table_prefix.'facebook_content_data',"","tag = '文章结尾' ","");
$_content_end_array=transform_2darray_in_1darray_with_new_key($_content_end,'nid','content');


$_content_tag=select_lines_all_items($table_prefix.'facebook_content_data',"","tag = '标签' ","");


$_content_chose_tag_array=$_REQUEST['tag'];

//print_r2($_content_chose_tag_array);

//有传递参数，则执行数据生成。
if($_REQUEST['creat_switch']=='on')
{

    $_content_head_str=implode("','",$_REQUEST['content_head']);
    $_content_chose_head=select_lines_all_items($table_prefix.'facebook_content_data',"","nid in ('{$_content_head_str}') ","");
    $_content_chose_head_nid_array=transform_2darray_in_1darray_number_as_key($_content_chose_head,'nid');
    $_content_chose_head_array=transform_2darray_in_1darray_with_new_key($_content_chose_head,'nid','content');



    $_content_body_str=implode("','",$_REQUEST['content_body']);
    $_content_chose_body=select_lines_all_items($table_prefix.'facebook_content_data',"","nid in ('{$_content_body_str}') ","");
    $_content_chose_body_nid_array=transform_2darray_in_1darray_number_as_key($_content_chose_body,'nid');
    $_content_chose_body_array=transform_2darray_in_1darray_with_new_key($_content_chose_body,'nid','content');


    $_content_end_str=implode("','",$_REQUEST['content_end']);
    $_content_chose_end=select_lines_all_items($table_prefix.'facebook_content_data',"","nid in ('{$_content_end_str}') ","");
    $_content_chose_end_nid_array=transform_2darray_in_1darray_number_as_key($_content_chose_end,'nid');
    $_content_chose_end_array=transform_2darray_in_1darray_with_new_key($_content_chose_end,'nid','content');


    $tag_amount=$_REQUEST['tag_amount'];

    foreach ($date_list as $k=>$date)
    {

        //随机获取这个数组的其中一条数据 $_content_head_array
        $_content_head_array_rand=$_content_chose_head_array[array_rand($_content_chose_head_array)];
        $_content_body_array_rand=$_content_chose_body_array[array_rand($_content_chose_body_array)];
        $_content_end_array_rand=$_content_chose_end_array[array_rand($_content_chose_end_array)];

        //从数组$_content_chose_tag_array中随机提取出$tag_amount个标签
        $_content_chose_tag_key_array_rand=array_rand($_content_chose_tag_array,$tag_amount);

        foreach ($_content_chose_tag_key_array_rand as $tag_key_rand)
        {
            $_result[$k]['tag']=$_result[$k]['tag']." ".$_content_chose_tag_array[$tag_key_rand];
        }



        $_result[$k]['content'] = $_content_head_array_rand . "\n" . $_content_body_array_rand . "\n" . $_content_end_array_rand. "\n";
        $_result[$k]['date']=$date;


    }
    //print_r2($_result);

//    print_r2($_content_head_array);
//    print_r2($_content_body_array);
//    print_r2($_content_end_array);
//
//
//    echo '要组装数据';
   // exit;

}









    //拆成数组，用于循环比较
    $wedding_tag_array = explode(";", $setting['facebook']['wedding_tag']);

    $now_data_for_wedding_tag_array=$_REQUEST['ai_wedding_tag'];



    //创建数组，值为80000001~80000030
    $media_list=array();
    for($i=1;$i<31;$i++){
        $media_list[]=800000+$i;
    }





    //获取文件夹列表
    $directoryPath = ROOT_PATH_ . "/uploadfiles/craft2tiangong";
    if (is_dir($directoryPath)) {
        //echo "目录存在".$directoryPath;
        //获取目录下的文件夹，直接写代码，赋值给变量
        function get_folder($directoryPath)
        {
            $dirs = array();
            if ($handle = opendir($directoryPath)) {
                while (false !== ($file = readdir($handle))) {
                    if ($file != "." && $file != "..") {
                        $dirs[] = $file;
                    }
                }
                closedir($handle);
            }
            return $dirs;
        }
        $picture_folder = get_folder($directoryPath);
    } else {
        echo "目录不存在";
    }


    //循环这些文件夹，看看里面有多少个文件
    $picture_folder_count=array();
    for($i=0;$i<count($picture_folder);$i++){
        $directoryPath = ROOT_PATH_ . "/uploadfiles/craft2tiangong/".$picture_folder[$i];
        if (is_dir($directoryPath)) {
           $_image_number[$picture_folder[$i]]=count(list_folder($directoryPath));
        }

    }


?>

